package com.chmingx.JavaBasic.thread.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Lock, ReentrantLock
 */
public class Ticket {
    private int number = 30;
    // 创建可重入锁
    private final ReentrantLock lock = new ReentrantLock();

    public void sale() {
        // 上锁
        lock.lock();
        try {
            if (number > 0) {
                number--;
                System.out.println(Thread.currentThread().getName() + " 卖出一张票, 还剩下： " + number);
            }
        } finally {
            // 解锁
            lock.unlock();
        }

    }
}
